import axios from 'axios'
import { getToken, setToken } from '@/utils/auth'
import { ElMessage } from 'element-plus'

// import { ElLoading } from 'element-plus';

// import store from '@/store'
// import qs from 'qs'

// import { getToken } from '@/utils/auth'

const timeOut = 20000;
// create an axios instance
const service = axios.create({

  // baseURL: "http://192.168.5.2:8013/", //process.env.VUE_APP_BASE_API, // url = base url + request url
   //baseURL: "http://localhost:2001/",//本地环境
  //baseURL: "https://test.jiangdaye.cn/",//服务器环境
  // baseURL: import.meta.env.VITE_BASE_URL,//正式服务器环境
  // baseURL: "https://xxl.xiedawang.top/",//正式服务器环境
  baseURL: "https://api.jiangdaye.cn/",//正式服务器环境
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: timeOut // request timeout
})

//let loadingInstance;
// request interceptor
service.interceptors.request.use(
  (config:any) => {
    // do something before request is sent
    //console.log(config);
    config.headers['Content-Type'] = 'application/json;charset=UTF-8',
    config.headers['Authorization'] = 'Bearer '+ getToken()
    return config;
  },
  (error:any) => {
    // loadingInstance.close();
    // do something with request error
    // console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  (response:any) => {
    const res = response.data;
    //  loadingInstance.close();
    // if the custom code is not 20000, it is judged as an error.
    //此处要进行后台请求代码的一些更改
    if (res.result === -401) {
      ElMessage({
        message: '请重新登录',
        type: 'error',
        duration: 3 * 1000
      })
      setToken('');
      setTimeout(() => {
        location.href = '/login';
      }, 100);
      return;
    }
    if (res.result === -500) {
      ElMessage({
        message: "服务器异常",
        type: 'error',
        duration: 3 * 1000
      })
      return;
    }
    if (res.result == -999) {
      ElMessage({
        message: res.message || 'Error',
        type: 'error',
        duration: 3 * 1000
      })
    } else if (res.result == -1000) {
      ElMessage({
        message: res.message,
        type: 'error',
        duration: 3 * 1000
      });
      setTimeout(() => {
        location.href = '/login';
      }, 1000);
    }
    return res

  },
  // (error:any) => {
  //   // loadingInstance.close();
  //   if (error.response) {
  //     if (error.response.status === 401) {
  //       //未登录,去进行登录
  //       // ElLoading.service({ fullscreen: true, text: "请重新登录", spinner: "el-icon-loading", background: 'rgba(0, 0, 0, 0.5)' });
  //       ElMessage({
  //         message: '请重新登录',
  //         type: 'error',
  //         duration: 3 * 1000
  //       })
  //       setTimeout(() => {
  //         location.href = '/login';
  //       }, 300);
  //       return;
  //     }
  //   } else {
  //     if (error.message === 'Network Error') {
  //       // location.reload();
  //       ElMessage({
  //         message: '远端服务器无响应',
  //         type: 'error',
  //         duration: 3 * 1000
  //       })
  //     } else {
  //       ElMessage({
  //         message: error.message,
  //         type: 'error',
  //         duration: 3 * 1000
  //       })
  //     }
  //     return Promise.reject(error)
  //   }
  // }
)

export default service
